﻿
Imports osi.root.delegates
Imports osi.root.formation
Imports osi.root.procedure
Imports osi.root.utils

Public Module _parameter_resolve
    Private Function parameter_resolve(Of FT)(ByVal r As Func(Of pointer(Of FT), event_comb),
                                              ByVal exec As Func(Of FT, event_comb)) As event_comb
        assert(Not r Is Nothing)
        assert(Not exec Is Nothing)
        Dim p As pointer(Of FT) = Nothing
        Dim ec As event_comb = Nothing
        Return New event_comb(Function() As Boolean
                                  p = New pointer(Of FT)()
                                  ec = r(p)
                                  Return waitfor(ec) AndAlso
                                         goto_next()
                              End Function,
                              Function() As Boolean
                                  If ec.end_result() AndAlso
                                     assert(Not +p Is Nothing) Then
                                      ec = exec(+p)
                                      Return waitfor(ec) AndAlso
                                                     goto_next()
                                  Else
                                      Return False
                                  End If
                              End Function,
                              Function() As Boolean
                                  Return ec.end_result() AndAlso
                                         goto_end()
                              End Function)
    End Function

    Public Function parameter_resolve(Of T,
                                         PT1,
                                         PT2) _
                                     (ByVal r As Func(Of PT1, 
                                                         pointer(Of Func(Of PT2, 
                                                                            pointer(Of T), 
                                                                            event_comb)), 
                                                         event_comb),
                                      ByVal p1 As PT1,
                                      ByVal p2 As PT2,
                                      ByVal o As pointer(Of T)) As event_comb
        assert(Not r Is Nothing)
        Return parameter_resolve(Function(x As pointer(Of Func(Of PT2, 
                                                                  pointer(Of T), 
                                                                  event_comb))) As event_comb
                                     Return r(p1, x)
                                 End Function,
                                 Function(x As Func(Of PT2, 
                                                       pointer(Of T), 
                                                       event_comb)) As event_comb
                                     assert(Not x Is Nothing)
                                     Return x(p2, o)
                                 End Function)
    End Function

    Public Function parameter_resolve(Of T,
                                         PT1,
                                         PT2,
                                         PT3) _
                                     (ByVal r As Func(Of PT1, 
                                                         pointer(Of Func(Of PT2, 
                                                                            PT3, 
                                                                            pointer(Of T), 
                                                                            event_comb)), 
                                                         event_comb),
                                      ByVal p1 As PT1,
                                      ByVal p2 As PT2,
                                      ByVal p3 As PT3,
                                      ByVal o As pointer(Of T)) As event_comb
        assert(Not r Is Nothing)
        Return parameter_resolve(Function(x As pointer(Of Func(Of PT2, 
                                                                  PT3, 
                                                                  pointer(Of T), 
                                                                  event_comb))) As event_comb
                                     Return r(p1, x)
                                 End Function,
                                 Function(x As Func(Of PT2, 
                                                       PT3, 
                                                       pointer(Of T), 
                                                       event_comb)) As event_comb
                                     assert(Not x Is Nothing)
                                     Return x(p2, p3, o)
                                 End Function)
    End Function

    Public Function parameter_resolve(Of T,
                                         PT1,
                                         PT2,
                                         PT3,
                                         PT4) _
                                     (ByVal r As Func(Of PT1, 
                                                         pointer(Of Func(Of PT2, 
                                                                            PT3, 
                                                                            PT4, 
                                                                            pointer(Of T), 
                                                                            event_comb)), 
                                                         event_comb),
                                      ByVal p1 As PT1,
                                      ByVal p2 As PT2,
                                      ByVal p3 As PT3,
                                      ByVal p4 As PT4,
                                      ByVal o As pointer(Of T)) As event_comb
        assert(Not r Is Nothing)
        Return parameter_resolve(Function(x As pointer(Of Func(Of PT2, 
                                                                  PT3, 
                                                                  PT4, 
                                                                  pointer(Of T), 
                                                                  event_comb))) As event_comb
                                     Return r(p1, x)
                                 End Function,
                                 Function(x As Func(Of PT2, 
                                                       PT3, 
                                                       PT4, 
                                                       pointer(Of T), 
                                                       event_comb)) As event_comb
                                     assert(Not x Is Nothing)
                                     Return x(p2, p3, p4, o)
                                 End Function)
    End Function

    Public Function parameter_resolve(Of T,
                                         PT1,
                                         PT2,
                                         PT3,
                                         PT4,
                                         PT5) _
                                     (ByVal r As Func(Of PT1, 
                                                         pointer(Of _func(Of PT2, 
                                                                             PT3, 
                                                                             PT4, 
                                                                             PT5, 
                                                                             pointer(Of T), 
                                                                             event_comb)), 
                                                         event_comb),
                                      ByVal p1 As PT1,
                                      ByVal p2 As PT2,
                                      ByVal p3 As PT3,
                                      ByVal p4 As PT4,
                                      ByVal p5 As PT5,
                                      ByVal o As pointer(Of T)) As event_comb
        assert(Not r Is Nothing)
        Return parameter_resolve(Function(x As pointer(Of _func(Of PT2, 
                                                                   PT3, 
                                                                   PT4, 
                                                                   PT5, 
                                                                   pointer(Of T), 
                                                                   event_comb))) As event_comb
                                     Return r(p1, x)
                                 End Function,
                                 Function(x As _func(Of PT2, 
                                                        PT3, 
                                                        PT4, 
                                                        PT5, 
                                                        pointer(Of T), 
                                                        event_comb)) As event_comb
                                     assert(Not x Is Nothing)
                                     Return x(p2, p3, p4, p5, o)
                                 End Function)
    End Function
End Module
